/* CORBAClient.cc -- Cliente CORBA
 *
 * Copyright (C) 2008, Diogo F. S. Ramos <diogo.ramos@usp.br>
 * GEAR - Grupo de Estudos Avançados em Robótica
 * <http://www.sel.eesc.usp.br/gear>
 *
 * This file is part of GEARMedula.
 * 
 * GEARMedula is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, version 3 of the
 * License.
 * 
 * GEARMedula is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 */

#include <iostream>

#include "GEARMedulaIDL.hh"
#include "CORBAClient.hh"

using namespace std;

CORBAClient::CORBAClient()
{
}

CORBAClient::~CORBAClient()
{
}

int
CORBAClient::Init( int argc, char * argv[] )
{
    try{
	
	CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);

	// Objeto Visao
	CORBA::Object_var obj =
	    orb->string_to_object(
		"corbaname::localhost#Visao.gm");
	if(CORBA::is_nil(obj)){
	    cerr << "Nill Time reference" << endl;
	    throw 0;
	}
	Visao_var vs = Visao::_narrow(obj);
	if( CORBA::is_nil(vs)){
	    cerr << "Argument is not a Time reference" << endl;
	    throw 0;
	}

	float field[LENGTH_FIELD_VECTOR];	
	for( int i = 0 ; i < LENGTH_FIELD_VECTOR ; i++ )
	    field[i] = 2.7;
	vs->setField( field );

	for( int i = 0 ; i < LENGTH_FIELD_VECTOR ; i++ )
	    field[i] = 56.87;
	vs->setField( field );

	float world[LENGTH_WORLD_VECTOR];	
	for( int i = 0 ; i < LENGTH_WORLD_VECTOR ; i++ )
	    world[i] = 78;
	vs->setWorld( world );
	
	// Objeto IA
	CORBA::Object_var obj_ia =
	    orb->string_to_object(
		"corbaname::localhost#IA.gm");
	if(CORBA::is_nil(obj_ia)){
	    cerr << "Nill Time reference" << endl;
	    throw 0;
	}
	IA_var ia = IA::_narrow(obj_ia);
	if( CORBA::is_nil(ia)){
	    cerr << "Argument is not a Time reference" << endl;
	    throw 0;
	}

	float field_resp[LENGTH_FIELD_VECTOR];
	ia->Read( field_resp );
	for( int i = 0 ; i < LENGTH_FIELD_VECTOR ; i++ )
	    cout << "field_resp[" << i << "]= " << field_resp[i] << endl;

	float world_resp[LENGTH_WORLD_VECTOR];
	ia->ReadWorld( world_resp );
	for( int i = 0 ; i < LENGTH_WORLD_VECTOR ; i++ )
	    cout << "world_resp[" << i << "]= " << world_resp[i] << endl;

	ia->SetVel( 0, 3.34, 5.32, 243.4 );

    }
    catch( const CORBA::Exception &)
    {
	cerr << "Uncaught CORBA exception" << endl;
	return 1;
    }
    catch(...)
    {
	return 1;
    }
    return 0;
}
